import {MsgMapper} from "../mappers/MsgMapper";
import {msgList} from "../api/msg";
import {copyObject} from "../util/objectUtils";
import {Msg} from "../sql/domin";
import {mapGetters, useStore} from "vuex";
import {computed, reactive} from "vue";
import audit from "../static/audit.png";
import {FeedClickHandle, FeedDataHandle} from "./feed";
import {inputService} from "./input";
import {AuditClickHandle} from "./audit";

export function msgService() {

    const store = useStore()

    const refreshMsg = async (id: number) => {
        await MsgMapper.deleteCheckedMsg(id)
        let msgs = await msgList()
        let datas = copyObject<Array<Msg>>(msgs.data.data)
        if (datas.length != 0) {
            await MsgMapper.insertMsg(datas as Msg[])
        }
    }

    const getMsgCount = async () => {
        let res = await MsgMapper.selectMsg({
            targetId: store.getters.userinfo.id
        }) as any[]
        store.dispatch('SET_MSG_COUNT', res.length)
    }

    return {
        refreshMsg,
        getMsgCount
    }
}

export function MsgIndex() {

    const {getList:getInputList} = inputService()
    const {getList:getFeedList} = FeedDataHandle()
    const {getList:getAuditList} = AuditClickHandle()

    const store = useStore()

    type MsgVo = {
        type: number,
        title: string,
        image: string,
        note: Array<Note>,
        url: string
    }

    type Note = {
        name: string,
        badge: number
    }

    const msgList = reactive<Array<MsgVo>>([
        {
            type: 1,
            title: '幼虫录入',
            image: '../../static/input.png',
            note: [],
            url: '/pages/input/input'
        },
        {
            type: 2,
            title: '正常饲养记录',
            image: '../../static/feed.png',
            note: [],
            url: '/pages/feed/feed'
        },
        {
            type: 3,
            title: '审批',
            image: audit,
            note: [],
            url: '/pages/audit/audit'
        }
    ])

    const adapetMsg = async ()=>{
        let res = await getMsgList() as Array<Msg>
        let warning = 0
        let alarm = 0
        res.map(value => {
            switch (value.msgType){
                case '1':
                    warning+=1;
                    break
                case '2':
                    alarm += 1
                    break
            }
        })
        if (warning != 0){
            msgList[1].note.push({
                name:'预警',
                badge:warning
            })
        }
        if (alarm != 0){
            msgList[1].note.push({
                name:'报警',
                badge:alarm
            })
        }
        getInputList().then(res=>{
            msgList[0].note.push({
                name:'待同步',
                badge:res.filter(it=>it.change != null).length
            })
        })
        getFeedList().then(res=>{
            msgList[1].note.push({
                name:'待同步',
                badge:res.filter(it=>it.change != null).length
            })
            msgList[1].note.push({
                name:'待记录',
                badge:res.filter(it=>it.feed.tableStatus == '0').length
            })
        })
        getAuditList().then(res=>{
            msgList[2].note.push({
                name:'待审批',
                badge:res.filter(it=>it.feed.tableStatus == '1').length
            })
            msgList[2].note.push({
                name:'待同步',
                badge:res.filter(it=>it.change != null).length
            })
        })
    }

    const getMsgList = async () => {
        return await MsgMapper.selectMsg({
            targetId: store.getters.userId,
            msgStatus: '0'
        })
    }

    return {
        msgList,
        adapetMsg
    }
}
